
package com.caration.shell.preference;

import android.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * A {@link preference} contains Icon
 * 
 * @author Lonn
 */
public class IconPreference extends Preference {
    private Drawable mIcon;

    public IconPreference(Context context) {
        this(context, null);
    }

    public IconPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        final View layout = createView();
        return layout;
    }

    /**
     * <p>
     * Create layout view
     * </p>
     * You must set
     * <p>
     * <b>android.R.widget_frame</b> as root view id
     * </p>
     * <p>
     * <b>android.R.title</b> as title textview id
     * </p>
     * <p>
     * <b>android.R.summary</b> as summary textview id
     * </p>
     * 
     * @return layout
     */
    protected View createView() {
        final int FILL = LayoutParams.FILL_PARENT;
        final int WRAP = LayoutParams.WRAP_CONTENT;

        LayoutParams fillParams = new LayoutParams(FILL, WRAP);
        LayoutParams wrapParams = new LayoutParams(WRAP, WRAP);
        final Context context = getContext();

        LinearLayout root = new LinearLayout(context);
        root.setId(R.id.widget_frame);
        root.setMinimumHeight(R.attr.listPreferredItemHeight);
        root.setGravity(Gravity.CENTER_VERTICAL);
        root.setPadding(0, 0, R.attr.scrollbarSize, 0);

        ImageView icon = new ImageView(context);
        icon.setId(R.id.icon);
        LayoutParams iconLp = new LayoutParams(wrapParams);
        iconLp.gravity = Gravity.CENTER;
        iconLp.setMargins(6, 0, 6, 0);

        RelativeLayout content = new RelativeLayout(context);
        LayoutParams contentLp = new LayoutParams(wrapParams);
        contentLp.weight = 1;
        contentLp.setMargins(2, 6, 6, 6);
        android.widget.RelativeLayout.LayoutParams relativeParams = new android.widget.RelativeLayout.LayoutParams(
                WRAP, WRAP);

        TextView title = new TextView(context);
        title.setId(android.R.id.title);
        title.setTextAppearance(context, R.attr.textAppearanceLarge);
        title.setEllipsize(TruncateAt.MARQUEE);
        title.setHorizontalFadingEdgeEnabled(true);
        title.setSingleLine();

        TextView summary = new TextView(context);
        summary.setId(android.R.id.summary);
        summary.setTextAppearance(context, R.attr.textAppearanceSmall);
        summary.setMaxLines(2);
        android.widget.RelativeLayout.LayoutParams summaryParams = new android.widget.RelativeLayout.LayoutParams(
                relativeParams);
        summaryParams.addRule(RelativeLayout.BELOW, R.id.title);
        summaryParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.title);

        content.addView(title, relativeParams);
        content.addView(summary, summaryParams);

        root.addView(icon, iconLp);
        root.addView(content, contentLp);
        return root;
    }

    public void onBindView(View v) {
        super.onBindView(v);
        ImageView icon = (ImageView) v.findViewById(R.id.icon);
        if ((icon != null) && (this.mIcon != null))
            icon.setImageDrawable(this.mIcon);
    }

    public void setIcon(Drawable drawable) {
        if (((drawable == null) && (this.mIcon != null))
                || ((drawable != null) && (!drawable.equals(this.mIcon)))) {
            this.mIcon = drawable;
            notifyChanged();
        }
    }

    public Drawable getIcon() {
        return this.mIcon;
    }
}
